import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/film',
    component: () => import('../views/Film'),
    children: [
      {
        path: '',
        redirect: 'nowplaying'
      },
      {
        path: 'nowplaying',
        component: () => import('../views/film/NowPlaying')
      },
      {
        path: 'comingsoon',
        component: () => import('../views/film/ComingSoon')
      }
    ]
  },
  {
    path: '/detail/:id', // TODO: 动态路由-记笔记
    name: 'Detail',
    component: () => import('../views/Detail')
  },
  {
    path: '/center',
    name: 'Center',
    component: () => import('../views/Center')
  },
  {
    path: '/cinema',
    name: 'Cinema',
    component: () => import('../views/Cinema')
  },
  {
    path: '/cinema/search',
    name: 'Search',
    component: () => import('../views/Search')
  },
  {
    path: '/city',
    name: 'City',
    component: () => import('../views/City')

  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '*', // 通配符 优先级最低
    redirect: '/film' // TODO: 页面重定向
  }
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})

// TODO: 全局导航守卫，或者叫路由拦截
router.beforeEach((to, from, next) => {
  const auth = ['/order', '/money']
  if (auth.includes(to.fullPath)) {
    console.log('验证token')
    next('/login')
  } else {
    next() // 放行，不拦截
  }
})

export default router
